package com.awspaass.user.apps.syncwechataddress;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import com.actionsoft.bpms.api.OpenApiClient;
import com.actionsoft.bpms.commons.database.ColumnMapRowMapper;
import com.actionsoft.bpms.schedule.IJob;
import com.actionsoft.bpms.util.DBSql;
import com.actionsoft.sdk.local.SDK;
import com.actionsoft.sdk.local.api.ORGAPI;
import com.actionsoft.sdk.service.response.StringResponse;
import com.awspaas.user.apps.shhtaerospaceindustrial.util.CoreUtil;

public class InitialPull implements IJob {
	
	public List<Map<String,String>> getAwsCompanyId(){
		List<Map<String,String>> cIdList = new ArrayList<Map<String,String>>();
		String querySql="select distinct(a.companyidaws),a.conmpanyidwechat  , a.role from BO_INFO_WECHAT_AWS a";
		List<Map<String, Object>> companyList = DBSql.query(querySql, new ColumnMapRowMapper(),
				new Object[] {});
		for(int i=0; i<companyList.size();i++) {
			Map<String,String> cInfo = new HashMap<String,String>();
			Map<String, Object> c = companyList.get(i);
			//String cId =  CoreUtil.objToStr(c.get("CONMPANYIDWECHAT"));
			String cIdAws = CoreUtil.objToStr(c.get("COMPANYIDAWS"));
			cInfo.put("CIDAWS", cIdAws);
			String cIdWechat = CoreUtil.objToStr(c.get("CONMPANYIDWECHAT"));
			cInfo.put("CIDWECHAT", cIdWechat);
			cInfo.put("ROLE", CoreUtil.objToStr(c.get("ROLE")));
			cIdList.add(cInfo);
		}
		return cIdList;
	}
	
	public List<Map<String,Object>> getAllUserInfo(String cIdAws){
		List<Map<String,Object>> userList = new ArrayList<Map<String,Object>>();
		String queryUserSql ="select a.USERID,a.USERNAME,a.DEPARTMENTID,a.MOBILE,a.EXT5 from orguser a ,orgdepartment b,orgcompany c where a.departmentid=b.id and b.companyid=c.id and c.id='"+ cIdAws+"'";
		userList = DBSql.query(queryUserSql, new ColumnMapRowMapper(),
				new Object[] {});
		
		return userList;
	}
	
	public List<Map<String,Object>> getAllWechatUserInfo(String cIdWechat){
		List<Map<String,Object>> userWechatList = new ArrayList<Map<String,Object>>();
		String querySql = "select * from WECHAT_USER a where a.companyid= '"+cIdWechat+ "'";
		userWechatList = DBSql.query(querySql, new ColumnMapRowMapper(),
				new Object[] {});
		return userWechatList;
		
	}
	public List<Map<String,Object>> getUpdateWechatUserInfo(String cIdWechat){
		List<Map<String,Object>> userWechatList = new ArrayList<Map<String,Object>>();
		String querySql = "select * from WECHAT_USER a where a.status ='1' and a.companyid= '"+cIdWechat+ "'";
		userWechatList = DBSql.query(querySql, new ColumnMapRowMapper(),
				new Object[] {});
		return userWechatList;
		
	}
	
	public String getDepAwsId(String depIdWechat, String companyIdWechat ) {
		String depIdAws = "";
		List<Map<String,Object>> depList = new ArrayList<Map<String,Object>>();
		String querySql = "select DEPIDAWS from BO_INFO_WECHAT_AWS t where t.idwechat='"+depIdWechat+ "' and t.conmpanyidwechat ='"+companyIdWechat+"'";
		
		depList = DBSql.query(querySql, new ColumnMapRowMapper(),
				new Object[] {});
		if(depList.size()>0) {
			depIdAws = depList.get(0).get("DEPIDAWS").toString();
		}
		return depIdAws;
	}
	@Override
	public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
		List<Map<String,String>> cIdList = getAwsCompanyId();
		for(int i =0; i<cIdList.size(); i++) {
			
			Map<String,String> cIdMap = cIdList.get(i);
			String cIdAws = cIdMap.get("CIDAWS");
			String role = cIdMap.get("ROLE");
			System.out.println("当前单位用户角色:"+role);
			System.out.println("当前单位AWSID:"+cIdAws);
			String cIdWechat = cIdMap.get("CIDWECHAT");
			List<Map<String,Object>> userInfoAwsList = getAllUserInfo(cIdAws);
			List<Map<String,Object>> userInfoWechatList = getAllWechatUserInfo(cIdWechat);
			List<Map<String,Object>> updateUserInfoWechatList = getUpdateWechatUserInfo(cIdWechat);
			System.out.println(userInfoAwsList.size());
			/*
			for(int j = 0; j < userInfoAwsList.size(); j++) {
				Map<String,Object> userAws = userInfoAwsList.get(j);
				String ext5="";
				if(userAws.get("EXT5")!=null) {
					ext5 = userAws.get("EXT5").toString();
				}

				String userNameAws =userAws.get("USERNAME").toString();
				String userIdAws = userAws.get("USERID").toString();
				if("admin".equals(userIdAws)) {
					continue;
				}
				int k =0;
				for(k=0; k< userInfoWechatList.size(); k++) {
					Map<String,Object> userWechat = userInfoWechatList.get(k);
					String useridW = userWechat.get("USERID").toString();
					
					if(ext5.equals(useridW)) {
						break;
					}
				}
				if(k==userInfoWechatList.size()) {
					System.out.println(userNameAws);
					System.out.println(userIdAws);
					//Map attrs=new HashMap<String,Object>();
					//attrs.put(userName, attrs)
					if(!userNameAws.contains("DISABLE")) {
						SDK.getORGAPI().updateUser(userIdAws, userNameAws+"DISABLE", ORGAPI.NO_UPDATE, ORGAPI.NO_UPDATE, ORGAPI.NO_UPDATE, ORGAPI.NO_UPDATE, ORGAPI.NO_UPDATE, ORGAPI.NO_UPDATE, ORGAPI.NO_UPDATE, ORGAPI.NO_UPDATE);
					}
					
				}
			}
			*/
			for(int j = 0; j < updateUserInfoWechatList.size(); j++) {
				Map<String,Object> userWechat = updateUserInfoWechatList.get(j);
				String userIdWechat = userWechat.get("USERID").toString();
				String userNameWechat = userWechat.get("USERNAME").toString();
				String MOBILE ="";
				if(userWechat.get("MOBILE")!=null) {
					MOBILE=userWechat.get("MOBILE").toString();
				}				
				String depIdWechat = userWechat.get("DEPID").toString();
				String companyIdWechat = userWechat.get("COMPANYID").toString();
				String depIdAws = getDepAwsId(depIdWechat,companyIdWechat);
				int k =0;
				for(k=0; k<userInfoAwsList.size(); k++) {
					Map<String,Object> userAws = userInfoAwsList.get(k);
					String ext5="";
					if(userAws.get("EXT5")!=null) {
						ext5 = userAws.get("EXT5").toString();
					}
					String userIdAws = userAws.get("USERID").toString(); 
					if(userIdWechat.equals(ext5)) {
						System.out.println(" 更新用户: "+depIdAws+"   "+userIdWechat);
						SDK.getORGAPI().updateUser(userIdAws,  ORGAPI.NO_UPDATE, ORGAPI.NO_UPDATE, ORGAPI.NO_UPDATE, MOBILE, ORGAPI.NO_UPDATE, ORGAPI.NO_UPDATE, ORGAPI.NO_UPDATE, ORGAPI.NO_UPDATE, ORGAPI.NO_UPDATE);
						break;
					}
				}
			
				if(k==userInfoAwsList.size()) {
					String ext5aws = userIdWechat;
					System.out.println(" 插入用户: "+depIdAws+"   "+userIdWechat);
					System.out.println("部门号："+depIdAws +"  部门角色:"+ role+"微信ID：  "+userIdWechat);
					if((!"DingYi".equals(userIdWechat))&&(!"13636689480".equals(userIdWechat))&&(!"17865816108".equals(MOBILE))) {
						SDK.getORGAPI().createUser(depIdAws, userIdWechat, userNameWechat, role, null, null, false, null,
								MOBILE, null, null, null, null, null, null, null, null, null, null, ext5aws, null);
					}


				}

			}
			
		}
		
		String queryAll = "select a.userid ,a.ext5, a.username,a.mobile,b.departmentname,c.ext2 from orguser a, orgdepartment b, orgcompany c ,wechat_user d where a.departmentid=b.id and b.companyid=c.id and a.ext5=d.userid and d.status=1";
		List<Map<String, Object>> userList = DBSql.query(queryAll, new ColumnMapRowMapper(),
				new Object[] {});
		OpenApiClient client = new OpenApiClient("https://47.100.109.206/portal/openapi", "opapiuser4326", "Abcd123456@#");
		for(int i = 0; i < userList.size(); i++) {
			Map<String,String> userInfo = new HashMap<String,String>();
			Map<String, Object> user = userList.get(i);
			String userId =  CoreUtil.objToStr(user.get("USERID"));
			String wechatId = CoreUtil.objToStr(user.get("EXT5"));
			String userName = CoreUtil.objToStr(user.get("USERNAME"));
			String mobile= CoreUtil.objToStr(user.get("MOBILE"));
			String departmentName = CoreUtil.objToStr(user.get("DEPARTMENTNAME"));
			String unitName = CoreUtil.objToStr(user.get("EXT2"));
			Map<String, Object> args = new HashMap<String, Object>();
			
			HashMap<String,Object> record=new HashMap<String,Object>();
			record.put("AWSUSERID", userId);
			record.put("WECHATUSERID", wechatId);
			record.put("MOBILE", mobile);
			record.put("UNIT", unitName);
			record.put("DEP", departmentName);
			record.put("USERNAME",userName);
			args.put("boName", "BO_EU_USER_INDEX");
			args.put("recordData", record);
			
			 client.exec("bo.data.create", args, StringResponse.class);
		}
		String updateSql = "UPDATE WECHAT_USER SET STATUS= '0' WHERE STATUS='1'";
		DBSql.update(updateSql);

	}
}
